package com.xintonghua.bussiness.ui.fragment.college;

import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.alibaba.fastjson.JSONObject;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.xcoder.lib.utils.LogUtils;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.CollectBean;
import com.xintonghua.bussiness.bean.CollegeDetail;
import com.xintonghua.bussiness.ui.MainActivity;
import com.xintonghua.bussiness.ui.view.PopupShare;
import com.xintonghua.bussiness.util.MyState;


/**
 * Created by Administrator on 2017/12/22.
 */

public class H5Activity extends BaseActivity implements PopupShare.MyClickListener {
    WebView web;
    CollectBean bean;
    private PopupShare popupShare;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_h5);
        popupShare = new PopupShare(this);
        popupShare.setMyClickListener(this);
        onSimpleActionBar();
        setSimpleActionBar("详情内容", R.mipmap.iv_share, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupShare.showPopupWindow();
            }
        });
        web = findViewById(R.id.h5);
        bean = getIntent().getParcelableExtra(BuildConfig.FLAVOR);
        setWebviewSetting(web.getSettings(), getApplicationContext().getCacheDir().getAbsolutePath());
        if (TextUtils.isEmpty(bean.getUrl())) {
            web.loadData(bean.getDetail(), "text/html; charset=UTF-8", null);
        } else {
            web.loadUrl(bean.getUrl() + "?id=" + bean.getId());
        }


        web.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();// 接受所有网站的证书
            }
        });
    }

    CollegeDetail collegeDetail;

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                collegeDetail = JSONObject.parseObject((String) data, CollegeDetail.class);
                setWebviewSetting(web.getSettings(), getApplicationContext().getCacheDir().getAbsolutePath());
                web.loadUrl(collegeDetail.getUrl());
                web.setWebViewClient(new WebViewClient());
                break;
        }
    }

    /**
     * 设置h5的样式
     */
    public static void setWebviewSetting(WebSettings setting, String appCachePath) {
        setting.setTextZoom(100);
        setting.setJavaScriptEnabled(true);
        setting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        //mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);  //设置 缓存模式
        setting.setCacheMode(WebSettings.LOAD_NO_CACHE);  //设置 缓存模式
        // 开启 DOM storage API 功能
        setting.setDomStorageEnabled(true);
        //开启 database storage API 功能
        setting.setDatabaseEnabled(true);
        //设置数据库缓存路径
        setting.setDatabasePath(appCachePath);
        //设置  Application Caches 缓存目录
        setting.setAppCachePath(appCachePath);
        //开启 Application Caches 功能
        setting.setAppCacheEnabled(true);
        //设置可以访问文件
        setting.setAllowFileAccess(true);
        //设置支持缩放
        setting.setBuiltInZoomControls(false);
        setting.setUseWideViewPort(true);
        setting.setLoadWithOverviewMode(true);
        setting.setBlockNetworkImage(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
    }


    @Override
    public void share(SHARE_MEDIA type) {
        if (!bean.getUrl().startsWith("http")) {
            bean.setUrl("http://" + bean.getUrl());
        }
        UMWeb web = new UMWeb(bean.getUrl() + "?id=" + bean.getId());
        web.setTitle(getString(R.string.app_name));//标题
        if (TextUtils.isEmpty(bean.getImg())) {
            web.setThumb(new UMImage(this, R.drawable.icon));  //缩略图
        } else {
            web.setThumb(new UMImage(this, bean.getImg()));  //缩略图
        }
        web.setDescription(bean.getTitle());//描述
        new ShareAction(H5Activity.this)
                .setPlatform(type)//传入平台
                .withMedia(web)//分享内容
                .setCallback(shareListener)//回调监听器
                .share();
    }
}
